/*
  Copyright (c) 2009-10 by kRaw1er & HierOS
  This file is part of M8J project
*/
#include "utils.h"

#define LOAD_STRING(lang, str)																	\
{																								\
	LoadString(MzGetInstanceHandle(), IDS_##lang##_##str, LANG_TEMP_STR, LANG_MAXSTR);			\
	(str) = LANG_TEMP_STR;																		\
}

void LoadLang(LANG lang)
{
	switch(lang)
	{
	case LANG_RU:
		LOAD_STRING(RU, STR_PRESENCE_AVAILABLE_TITLE);
		LOAD_STRING(RU, STR_PRESENCE_UNAVAILABLE_TITLE);
		LOAD_STRING(RU, STR_PRESENCE_AWAY_TITLE);
		LOAD_STRING(RU, STR_PRESENCE_CHAT_TITLE);
		LOAD_STRING(RU, STR_PRESENCE_DND_TITLE);
		LOAD_STRING(RU, STR_PRESENCE_XA_TITLE);
		LOAD_STRING(RU, STR_PRESENCE_UNKNOWN_TITLE);
		LOAD_STRING(RU, STR_INPUT_JID_TIP);
		LOAD_STRING(RU, STR_INPUT_PWD_TIP);
		LOAD_STRING(RU, STR_INPUT_RSC_TIP);
		LOAD_STRING(RU, STR_INPUT_JID_TIP2);
		LOAD_STRING(RU, STR_INPUT_PWD_TIP2);
		LOAD_STRING(RU, STR_INPUT_RSC_TIP2);
		LOAD_STRING(RU, STR_BACK);
		LOAD_STRING(RU, STR_DONE);
		LOAD_STRING(RU, STR_SEND);
		LOAD_STRING(RU, STR_MESSAGE);
		LOAD_STRING(RU, STR_EXIT);
		LOAD_STRING(RU, STR_ACCOUNT);
		break;
		
	case LANG_EN:
		LOAD_STRING(EN, STR_PRESENCE_AVAILABLE_TITLE);
		LOAD_STRING(EN, STR_PRESENCE_UNAVAILABLE_TITLE);
		LOAD_STRING(EN, STR_PRESENCE_AWAY_TITLE);
		LOAD_STRING(EN, STR_PRESENCE_CHAT_TITLE);
		LOAD_STRING(EN, STR_PRESENCE_DND_TITLE);
		LOAD_STRING(EN, STR_PRESENCE_XA_TITLE);
		LOAD_STRING(EN, STR_PRESENCE_UNKNOWN_TITLE);
		LOAD_STRING(EN, STR_INPUT_JID_TIP);
		LOAD_STRING(EN, STR_INPUT_PWD_TIP);
		LOAD_STRING(EN, STR_INPUT_RSC_TIP);
		LOAD_STRING(EN, STR_INPUT_JID_TIP2);
		LOAD_STRING(EN, STR_INPUT_PWD_TIP2);
		LOAD_STRING(EN, STR_INPUT_RSC_TIP2);
		LOAD_STRING(EN, STR_BACK);
		LOAD_STRING(EN, STR_DONE);
		LOAD_STRING(EN, STR_SEND);
		LOAD_STRING(EN, STR_MESSAGE);
		LOAD_STRING(EN, STR_EXIT);
		LOAD_STRING(EN, STR_ACCOUNT);
		break;
	}
}

#define LOAD_IMG(img) (img).LoadImageFromRes(MzGetInstanceHandle(), RT_RCDATA, MAKEINTRESOURCE(IDB_##img), true, true);
void LoadGraphics()
{
	LOAD_IMG(IMG_PRESENCE_AVAILABLE_NORMAL);
	LOAD_IMG(IMG_PRESENCE_AVAILABLE_PRESSED);
	LOAD_IMG(IMG_INCOMING_MESSAGE);
	LOAD_IMG(IMG_OUTCOMING_MESSAGE);
	LOAD_IMG(IMG_OPTIONS);
	LOAD_IMG(IMG_CHAT);
	LOAD_IMG(IMG_EXIT);
	LOAD_IMG(IMG_ONLINE);
	LOAD_IMG(IMG_OFFLINE);
	LOAD_IMG(IMG_AWAY);
	LOAD_IMG(IMG_FFC);
	LOAD_IMG(IMG_XA);
	LOAD_IMG(IMG_DND);
	LOAD_IMG(IMG_CAPT);
}

std::wstring fromUTF8(const std::string &str)
{
	std::wstringstream str_stream;

	int iLength = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, NULL);

	wchar_t * tmp = new wchar_t[iLength];

	MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, tmp, iLength);

	str_stream << tmp;

	delete[] tmp;

	return str_stream.str();
}

std::string toUTF8(const std::wstring &str)
{
	std::stringstream str_stream;

	int iSize = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL);

	char * tmp = new char[iSize];

	WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, tmp, iSize, NULL, NULL);

	str_stream << tmp;

	delete[] tmp;

	return str_stream.str();
}

std::string toLower(const std::string &str)
{
	std::wstring twstr;
	std::stringstream str_stream;

	twstr = fromUTF8(str);

	int nLength = twstr.length() + 1;

	wchar_t * tmp = new wchar_t[nLength];
	wsprintf(tmp, L"%s", twstr.c_str());
	_wcslwr_s(tmp, nLength);
	
	str_stream << toUTF8(tmp);

	delete [] tmp;

	return str_stream.str();
}

std::wstring toLower(const std::wstring &str)
{
	std::wstringstream str_stream;

	int nLength = str.length() + 1;

	wchar_t * tmp = new wchar_t[nLength];
	wsprintf(tmp, L"%s", str.c_str());
	_wcslwr_s(tmp, nLength);
	
	str_stream << tmp;

	delete [] tmp;

	return str_stream.str();
}

bool IsToday(SYSTEMTIME time)
{
	SYSTEMTIME ttime;
	GetLocalTime(&ttime);
	return (time.wDay == ttime.wDay && time.wMonth == ttime.wMonth && time.wYear == ttime.wYear);
}

CMzString GetMyPath()
{
    HMODULE hmodule;
    hmodule = GetModuleHandleW(NULL);
    WCHAR path[MAX_PATH];
    int i = GetModuleFileNameW(hmodule, path, MAX_PATH);

    for (; i >= 0 ; i--)
    {
        if (path[i] == L'\\')
        {
            path[i] = 0;
            break;
        }
    }
    return CMzString(path);
}
CMzString accIniFile()
{
	CMzString accinifile = GetMyPath() + L"\\Account.ini";
    return CMzString(accinifile);
}
CMzString optIniFile()
{
	CMzString optinifile = GetMyPath() + L"\\Options.ini";
    return CMzString(optinifile);
}
bool RegReadDWORD(IN HKEY const hKeyName,IN LPCTSTR lpSubKey,IN LPCTSTR lpValueName,OUT DWORD &dwValue)
{
	HKEY	hKey = NULL;
	DWORD dwSize=sizeof(dwValue);
	if (ERROR_SUCCESS==::RegOpenKeyEx(hKeyName,lpSubKey, 0, NULL,&hKey))
	{
		LONG lQueryRe=::RegQueryValueEx(hKey, lpValueName, NULL,NULL,(BYTE *)&dwValue,&dwSize);	
		::RegCloseKey(hKey);

		if (ERROR_SUCCESS==lQueryRe)
		{
			return true;
		} 
		else
		{
			return false;
		}

	}
	return false;
}
bool RegWriteDWORD(IN HKEY const hKeyName,IN LPCTSTR lpSubKey,IN LPCTSTR lpValueName,IN DWORD const dwValue,IN bool bCreateKey)
{
 HKEY hKey = NULL;
 LONG lRet=0;
 if (bCreateKey)
 {
  DWORD dwDisposition = 0;  //REG_CREATED_NEW_KEY, REG_OPENED_EXISTING_KEY
  lRet=::RegCreateKeyEx(hKeyName, lpSubKey, 0, NULL,REG_OPTION_NON_VOLATILE ,0 , NULL, &hKey, &dwDisposition);
 }
 else
 {
  lRet=::RegOpenKeyEx(hKeyName,lpSubKey, 0, NULL,&hKey);
 }
 if (ERROR_SUCCESS==lRet)
 {
  ::RegSetValueEx(hKey,lpValueName, 0, REG_DWORD,(BYTE *)&dwValue, sizeof(dwValue)); 
  ::RegCloseKey(hKey);
  return true;
 }
 return false;
}